package com.gs.djys.checkfragments.approval;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.gs.djys.Base.MyAppliCation;
import com.gs.djys.R;
import com.gs.djys.Util.NoRecycleView;
import com.gs.djys.Util.ToastUtils;
import com.gs.djys.adapter.LGRecycleViewAdapter;
import com.gs.djys.adapter.LGViewHolder;
import com.gs.djys.mvp.MVPBaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class ApprovalFragment extends MVPBaseFragment<ApprovalContract.View, ApprovalPresenter> implements ApprovalContract.View {

    @Bind(R.id.sczt_te)
    TextView scztTe;
    @Bind(R.id.sczt_lay)
    LinearLayout scztLay;
    @Bind(R.id.listview)
    NoRecycleView listview;
    @Bind(R.id.rv_logistics)
    RecyclerView rvLogistics;
    @Bind(R.id.activity_disease_detail_scrollview)
    ScrollView activityDiseaseDetailScrollview;

    @Override
    public void onRequestError(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fra_approval, container, false);
        ButterKnife.bind(this, view);
        initdata();
        return view;
    }

    private void initdata() {
        if (MyAppliCation.spUtils.getString("REQ_NO").startsWith("GS")) {
            mPresenter.getGSProces(MyAppliCation.spUtils.getString("REQ_NO"));
            scztTe.setVisibility(View.GONE);
            scztLay.setVisibility(View.GONE);
            listview.setVisibility(View.GONE);
        } else {
            mPresenter.getLink(MyAppliCation.spUtils.getString("REQ_NO"));
            mPresenter.getProces(MyAppliCation.spUtils.getString("REQ_NO"));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void getLinks(ApprovalBean landBean) {
        if (landBean.getData()!=null){
            if (landBean.getData().getRecords()!=null&&landBean.getData().getRecords().size()>0){
                LinearLayoutManager manager = new LinearLayoutManager(getContext());
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                listview.setLayoutManager(manager);
                LGRecycleViewAdapter<ApprovalBean.DataBean.RecordsBean> adapter = new LGRecycleViewAdapter<ApprovalBean.DataBean.RecordsBean>(landBean.getData().getRecords()) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.approva_pro_item;
                    }

                    @Override
                    public void convert(LGViewHolder holder, ApprovalBean.DataBean.RecordsBean zCDBean, int position) {
                        holder.setText(R.id.one, zCDBean.getDistName());
                        holder.setText(R.id.two, zCDBean.getReviewFlag());
                        holder.setText(R.id.three, zCDBean.getReviewComment());
                        holder.setText(R.id.four, zCDBean.getReviewTime());
                    }
                };
                listview.setAdapter(adapter);
            }
        }
    }

    @Override
    public void getProcess(ProcesBean landBean) {
        if (landBean.getData()!=null){
            if (landBean.getData().getRecords()!=null&&landBean.getData().getRecords().size()>0){
                List<LogisticsInfoBean> data = new ArrayList<>();
                for (int i = 0; i < landBean.getData().getRecords().size(); i++) {
                    LogisticsInfoBean logisticsInfoBean = new LogisticsInfoBean();
                    logisticsInfoBean.setAcceptTime(landBean.getData().getRecords().get(i).getOPT_TIME());
                    logisticsInfoBean.setAcceptStation("【" + landBean.getData().getRecords().get(i).getOPT_BY() + "】：" + landBean.getData().getRecords().get(i).getOPT_STATUS() + "\n【办理意见】：" + landBean.getData().getRecords().get(i).getOPT_DESC());
                    data.add(logisticsInfoBean);
                }
                rvLogistics.setLayoutManager(new LinearLayoutManager(getActivity()));
                rvLogistics.setFocusable(false);
                //解决ScrollView嵌套RecyclerView出现的系列问题
                rvLogistics.setNestedScrollingEnabled(false);
                rvLogistics.setHasFixedSize(true);
                rvLogistics.setAdapter(new LogisticsInfoAdapter(getActivity(), R.layout.item_approva_pro, data));
            }
        }
    }

    @Override
    public void getGSProces(GSProcesBean landBean) {
        if (landBean.getData()!=null){
            if (landBean.getData().getRecords()!=null&&landBean.getData().getRecords().size()>0){
                List<LogisticsInfoBean> data = new ArrayList<>();
                for (int i = 0; i < landBean.getData().getRecords().size(); i++) {
                    LogisticsInfoBean logisticsInfoBean = new LogisticsInfoBean();
                    logisticsInfoBean.setAcceptTime(landBean.getData().getRecords().get(i).getOPT_TIME());
                    logisticsInfoBean.setAcceptStation(landBean.getData().getRecords().get(i).getUPDATE_BY() + "【" + landBean.getData().getRecords().get(i).getOPT_CODE_NAME() + "】" + "\n【结论】：" + landBean.getData().getRecords().get(i).getOPT_DESC());
                    data.add(logisticsInfoBean);
                }
                rvLogistics.setLayoutManager(new LinearLayoutManager(getActivity()));
                rvLogistics.setFocusable(false);
                //解决ScrollView嵌套RecyclerView出现的系列问题
                rvLogistics.setNestedScrollingEnabled(false);
                rvLogistics.setHasFixedSize(true);
                rvLogistics.setAdapter(new LogisticsInfoAdapter(getActivity(), R.layout.item_approva_pro, data));
            }
        }
    }
}
